package com.symbio.ishenapi.background.controller;

import com.jfinal.kit.StrKit;
import com.symbio.ishenapi.background.kit.JdPush;
import com.symbio.ishenapi.background.model.TmNews;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description:站内信管理控制层
 * @Date: Created in 16:23 2018/10/22
 * @Modified By:
 */

@RequestMapping("/TmNews")
public class TmNewsController extends BaseController {


    /**
     * 站内信列表查询
     */
    public void findList(){

        Map map = TmNews.dao.findList(getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    public void save(){
        TmNews tmNews = (TmNews) getReqBodyObject(TmNews.class);
        //判断是否发送短信 0.不发送 1.发送
        if(tmNews.getSendMsg()!=null && tmNews.getSendMsg()==1){
            Map<String,String> param = new HashMap<>();
            param.put("title","");
            param.put("msg",tmNews.getContent());
            //if 判断发送哪个端  tmNews.getSendClient()  1.医生端 2.患者端
            if(!StrKit.isBlank(tmNews.getSendClient())){
                String[] clients = tmNews.getSendClient().split(",");
                for(int i=0;i<clients.length;i++){
                    switch (clients[i]){
                        case "1":
                            param.put("client","1");
                            JdPush.jpushAndroid(param);
                            break;
                        case "2":
                            param.put("client","2");
                            JdPush.jpushAndroid(param);
                            break;
                            default:
                                param.put("client","1");
                                JdPush.jpushAndroid(param);
                                param.put("client","2");
                                JdPush.jpushAndroid(param);
                                break;
                    }
                }
            }
        }else {
            tmNews.setSendClient("");
        }

        //填充数据
        tmNews.setRevJson("{}");
        //获取登录人信息(待补充)
        tmNews.setCreateBy("admin");
        tmNews.setCreateTime(new Date());
        tmNews.setUpdateBy("admin");
        tmNews.setUpdateTime(new Date());
        tmNews.setStatus(1);

        if(tmNews.getContent()==null || "".equals(tmNews.getContent())){
            renderJsonError(40003, "发布内容");
        }
        if (tmNews.save()){
            renderJsonRet(40001);
        }else {
            renderJsonError(40002);
        }
    }
}
